'use strict';

const Controller = require('egg').Controller;
const MockData = require('../mock/index')

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //模拟数据
  async mock() {
    const { ctx, app } = this;
    await MockData.map((item) => {
      app.mysql.query(`insert into tabelist(img,title,sold,time,num,count,name,solp,slie,cope,type,type1,type2) values("${item.img}","${item.title}","${item.sold}","${item.time}","${item.num}","${item.count}","${item.name}","${item.solp}","${item.slie}","${item.cope}","${item.type}","${item.type1}","${item.type2}")`)
    })
    console.log(MockData);

    ctx.body = {
      code: 200,
      msg: "成功",
      MockData
    }
  }

  //渲染数据
  async getList() {
    const { ctx, app } = this;
    const data = await app.mysql.select('tabelist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //筛选
  async area() {
    const { ctx, app } = this;
    const { screen } = ctx.request.body;
    const data = await app.mysql.select('order', { where: { type: screen } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }
  //筛选
  async area1() {
    const { ctx, app } = this;
    const { screen1 } = ctx.request.body;
    const data = await app.mysql.select('order', { where: { type1: screen1 } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }
  //筛选
  async area2() {
    const { ctx, app } = this;
    const { screen2 } = ctx.request.body;
    const data = await app.mysql.select('order', { where: { type2: screen2 } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }
  //搜索
  async search() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where sold like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //搜索
  async search2() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where wifelist like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //搜索
  async search3() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where stoplist like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //搜索
  async search1() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where skinge like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //搜素
  async search4() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where brealist like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //详情
  async detail() {
    const { ctx, app } = this;
    const data = await app.mysql.get('tabelist', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //加入购物车
  async add() {
    const { ctx, app } = this;
    const { info } = ctx.request.body;
    const data = await app.mysql.get('shop', { id: info.id })
    if (data) {
      await app.mysql.update('shop', { id: info.id, sum: data.sum + 1 })
    } else {
      await app.mysql.insert('shop', { id: info.id, sking: info.sking, skinge: info.skinge, skprice: info.skprice, sktype: info.sktype, sum: info.sum, type: info.type, type1: info.type1, type2: info.type2 })
    }
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //购物车渲染
  async addlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shop')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //删除
  async delete() {
    const { ctx, app } = this;
    const data = await app.mysql.delete('shop', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  //删除地址
  async delcity() {
    const { ctx, app } = this;
    const data = await app.mysql.delete('add', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  async carIs() {
    const { ctx, app } = this;
    const { id, is } = ctx.request.body
    await app.mysql.update('shop', { id, is })
    ctx.body = {
      code: 200,
    }
  }


  async carItem() {
    const { ctx, app } = this;
    const { id, sum } = ctx.request.body
    await app.mysql.update('shop', { id: id, sum: sum })
    ctx.body = {
      code: 200,
      msg: '成功'
    }
  }


  async drug() {
    const { ctx, app } = this;
    const data = await app.mysql.select('drug')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  //删除
  async deletedrug() {
    const { ctx, app } = this;
    const data = await app.mysql.delete('drug', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  async druglist() {
    const { app, ctx } = this
    const { name } = ctx.request.body
    const data = await app.mysql.insert('drug', { name })
    ctx.body = {
      code: 1,
      data
    }
  }

  //加入订单
  async order() {
    const { ctx, app } = this;
    const { item } = ctx.request.body;
    const data = await app.mysql.get('order', { id: item.id })
    if (data) {
      await app.mysql.update('order', { id: item.id })
    } else {
      await app.mysql.insert('order', { id: item.id, sking: item.sking, skinge: item.skinge, skprice: item.skprice, sktype: item.sktype, type: item.type, type1: item.type1, type2: item.type2 })
    }
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }
  //订单渲染
  async orderlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('order')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async del() {
    const { ctx, app } = this
    const data = await app.mysql.delete('order', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: '成功',
      data
    }
  }

  async address() {
    const { app, ctx } = this
    const { name, phone, city, detailcity } = ctx.request.body
    const data = await app.mysql.insert('add', { name, phone, city, detailcity })
    ctx.body = {
      code: 1,
      data
    }
  }

  async addresslist() {
    const { app, ctx } = this;
    const data = await app.mysql.select('add')
    ctx.body = {
      code: 200,
      msg: '成功',
      data
    }
  }


  async one() {
    const { app, ctx } = this
    const { number, stature, weight, BMI, waistline, smoke } = ctx.request.body
    const data = await app.mysql.insert('one', { number, stature, weight, BMI, waistline, smoke })
    ctx.body = {
      code: 1,
      data
    }
  }

  async onelist() {
    const { app, ctx } = this
    const data = await app.mysql.select('one')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

}

module.exports = HomeController;
